﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.WebPages;
using System.Web.WebPages.Html;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Text;
using System.Text.RegularExpressions;


namespace LightSO.CMS.Model.EasyUI
{
    public static class HtmlExtension
    {
        /// <summary>
        /// 生成标准按钮（视图中调用）
        /// </summary>
        /// <param name="helper">System.Web.Mvc.HtmlHelper</param>
        /// <param name="id">id</param>
        /// <param name="text">显示文字</param>
        /// <param name="onclick">点击事件触发的JS脚本</param>
        /// <param name="icon">图标</param>
        /// <param name="visible">按钮显示状态，为true时表示审核通过显示按钮，为false时表示审核不通过显示按钮</param>
        /// <returns>HTML字符串</returns>
        public static IHtmlString Button(this System.Web.Mvc.HtmlHelper helper, string id, string text, string onclick, string icon, bool visible = true, bool plain = true)
        {
            Button button = new Button(id, text, onclick, icon, plain);
            if (!helper.CheckButtonRight(onclick, ref button, visible))
                return helper.Raw(string.Empty);

            var html = button.ToHtml();

            return helper.Raw(html);
        }

        /// <summary>
        /// 生成菜单按钮（视图中调用）
        /// </summary>
        /// <param name="helper">System.Web.Mvc.HtmlHelper</param>
        /// <param name="id">id</param>
        /// <param name="text">显示文字</param>
        /// <param name="onclick">点击事件触发的JS脚本</param>
        /// <param name="icon">图标</param>
        /// <param name="audit">按钮显示状态，为true时表示审核通过显示按钮，为false时表示审核不通过显示按钮</param>
        /// <returns>HTML字符串</returns>
        public static IHtmlString Menu(this System.Web.Mvc.HtmlHelper helper, string id, string text, string onclick, string icon, bool visible = true, bool plain = true)
        {
            PopMenuItem menu = new PopMenuItem(id, text, onclick, icon, plain);
            if (!helper.CheckPopMenuItemRight(onclick, ref menu, plain, visible))
                return helper.Raw(string.Empty);

            var html = (new PopMenuItem(id, text, onclick, icon, plain)).ToHtml();

            return helper.Raw(html);
        }

        /// <summary>
        /// 生成枚举列表
        /// </summary>
        /// <param name="helper">ystem.Web.Mvc.HtmlHelper</param>
        /// <param name="template">HTML模版（如：<option value='{1}'>{0}</option>, 0:Enum的当前Language, 1:Enum值）</param>
        /// <returns></returns>
        public static IHtmlString RenderEnums<T>(this System.Web.Mvc.HtmlHelper helper, string template, int? selectedValue = null)
        {
            var sb = new StringBuilder();
            if (typeof(T).BaseType.Equals(typeof(Enum)))
            {
                var values = Enum.GetValues(typeof(T));
                foreach (var item in values)
                {
                    var val = item as Enum;
                    if (selectedValue != null && selectedValue == Convert.ToInt32(item))
                    {
                        Regex regex = new Regex("value=['\"]\\{1\\}['\"]");
                        var templ = regex.Replace(template, "value='{1}' selected='selected'");
                        sb.AppendFormat(templ, val.Text(), Convert.ToInt32(item));
                    }
                    else
                        sb.AppendFormat(template, val.Text(), Convert.ToInt32(item));
                }
            }

            return helper.Raw(sb.ToString());
        }

        /// <summary>
        /// 取得枚举文件
        /// </summary>
        /// <param name="em">枚举值</param>
        /// <returns></returns>
        public static string Text(this Enum em)
        {
            return em.ToString();
        }

        /// <summary>
        /// 生成JS枚举
        /// var CustomerType = new array();
        /// CustomerType[1] = "XXXXXXX";
        /// （方便JS调用枚举）
        /// </summary>
        /// <param name="type">Enum类型</param>
        /// <param name="helper">System.Web.Mvc.HtmlHelper</param>
        /// <returns>HTML字符串</returns>
        public static IHtmlString EnumDefineScript(this System.Web.Mvc.HtmlHelper helper, Type type)
        {
            var sb = new StringBuilder();
            if (type.BaseType.Equals(typeof(Enum)))
            {
                var culture = System.Globalization.CultureInfo.CurrentCulture.Name;
                var vals = Enum.GetValues(type);
                foreach (var val in vals)
                    sb.AppendFormat("{0}[{1}] = '{2}';\r\n", type.Name, (int)val, ((Enum)val).Text());

                if (sb.Length > 0)
                    sb.Insert(0, string.Format("var {0} = new Array();\r\n", type.Name));
            }
            return helper.Raw(sb.ToString());
        }

        /// <summary>
        /// 检查按钮权限
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="onclick"></param>
        /// <param name="button"></param>
        /// <param name="visibile">该按钮是否可见，为true时表示显示按钮，为false时表示不显示按钮</param>
        /// <param name="idField">读取记录对应的审核状态所用到的Id（employer.id或hunter.id或jobseeker.id或position.id）</param>
        /// <returns></returns>
        private static bool CheckButtonRight(this System.Web.Mvc.HtmlHelper helper, string onclick, ref Button button, bool visible)
        {
            if (!visible) return false;

            return true;
        }

        /// <summary>
        /// 检查弹出菜单权限
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="onclick"></param>
        /// <param name="menuItem"></param>
        /// <param name="audit">审核状态，为true时表示审核通过显示按钮，为false时表示审核不通过显示按钮</param>
        /// <param name="idField">读取记录对应的审核状态所用到的Id（employer.id或hunter.id或jobseeker.id或position.id）</param>
        /// <returns></returns>
        private static bool CheckPopMenuItemRight(this System.Web.Mvc.HtmlHelper helper, string onclick, ref PopMenuItem menuItem, bool plain, bool visible)
        {
            if (!visible) return false;

            return true;
        }
    }
}